home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / EXECPRNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  2.1 KB  |  87 lines

  1. /* execprnt.c --- BIBLE pp. 77-83 */
  2. /* ====================== PARENT ========================= */
  3. #include <stdio.h>
  4. #include <process.h>
  5. #include <alloc.h>
  6. #include <string.h>
  7. typedef struct TEST_DATA
  8. {
  9.     char name[20];
  10.     int n;
  11.     double x;
  12. } TEST_DATA;
  13.                     /* PARENT:  Test the "exec" functions.  Pass address of
  14.                      *         data in command line arguments as well as
  15.                      *        environment variables when appropriate. */
  16. char *envp[] =
  17. {
  18.     "PARENT=EXEC FUNCTIONS",
  19.     NULL
  20. };
  21. main()
  22. {
  23.     char *argv[4],buf[20], rname[40];
  24.     TEST_DATA *pdata;
  25.                     /* Set up a data structure and initialize it */
  26.     if((pdata=(TEST_DATA *)
  27.         malloc(sizeof(TEST_DATA))) == NULL) abort();
  28.     strcpy(pdata->name, "PARENT");
  29.     pdata->n = 100;
  30.     pdata->x = 1000.99;
  31.                     /* Set up the arguments for the child process */
  32.     argv[0] = "child.exe",
  33.     argv[1] = rname;
  34.     sprintf(buf, "%Fp", (void far *)pdata);
  35.     argv[2] = buf;
  36.     argv[3] = NULL;
  37.                         /* Ask user which "exec" routine to call */
  38.     printf("Enter name of \"exec\" function to call:");
  39.     gets(rname);
  40.     strlwr(rname);
  41.                     /* Call the "exec" function requested by the user */
  42.     if(strcmp(rname, "execl") == 0)
  43.     {
  44.         execl("child.exe", "execl", buf, NULL);
  45.     }
  46.     if(strcmp(rname, "execle") == 0)
  47.     {
  48.         execle("child.exe",
  49.         "child.exe", "execle", buf, NULL, envp);
  50.     }
  51.     if(strcmp(rname, "execlp") == 0)
  52.     {
  53.         execlp("child.exe",
  54.         "child.exe", "execlp", buf, NULL);
  55.     }
  56.     if(strcmp(rname, "execlpe") == 0)
  57.     {
  58.         execlpe("child.exe",
  59.         "child.exe", "execlpe", buf, NULL, envp);
  60.     }
  61.     if(strcmp(rname, "execv") == 0)
  62.     {
  63.         execv("child.exe", argv);
  64.     }
  65.     if(strcmp(rname, "execve") == 0)
  66.     {
  67.         execve("child.exe", argv, envp);
  68.     }
  69.     if(strcmp(rname, "execvp") == 0)
  70.     {
  71.         execvp("child.exe", argv);
  72.     }
  73.     if(strcmp(rname, "execvpe") == 0)
  74.     {
  75.         execvpe("child.exe", argv, envp);
  76.     }
  77.                                 /* Check if we could call child or not */
  78.     if(strcmp(pdata->name, "CHILD") == 0)
  79.     {
  80.         printf("Back from child: name = %s, n = %d, x = %f\n",
  81.                                 pdata->name, pdata->n, pdata->x);
  82.     }
  83.     else
  84.     {
  85.         printf("Don't know:  %s\n", rname);
  86.     }
  87. }